import { GoodsImageService } from './../../../service/goods-image/goods-image.service';
import { ResponseInterceptor } from './../../../interceptor/response/response.interceptor';
import { GoodsService } from 'src/service/goods/goods.service';
import { Controller, Get, Query, UseInterceptors, BadRequestException } from '@nestjs/common';
import config from 'src/config/config';
import * as mongoose from 'mongoose';

@Controller(`${config.shopApiPath}/product`)
@UseInterceptors(ResponseInterceptor)
export class ProductController {
    constructor(private readonly goodsService: GoodsService, private readonly goodsImageService: GoodsImageService) {}
    @Get()
    async getProduct(@Query() query) {
        const { _id } = query;
        if (!_id) throw new BadRequestException('请传商品id');
        const result = await this.goodsService.getModel().aggregate([
            { $match: { _id: new mongoose.Types.ObjectId(_id) } },
            { $lookup: { localField: '_id', foreignField: 'goods_id', from: 'goods_image', as: 'goods_image_list' } },
            { $lookup: { localField: 'relation_goods', foreignField: '_id', from: 'goods', as: 'relation_goods' } },
            { $lookup: { localField: 'goods_gift', foreignField: '_id', from: 'goods', as: 'goods_gift' } },
            { $lookup: { localField: 'goods_color', foreignField: '_id', from: 'goods_color', as: 'goods_color' } },
            {
                $lookup: {
                    localField: '_id',
                    foreignField: 'goods_id',
                    from: 'goods_attr',
                    as: 'attributes',
                    pipeline: [
                        {
                            $lookup: {
                                localField: 'attribute_id',
                                foreignField: '_id',
                                from: 'goods_type_attribute',
                                as: 'type_attribute',
                            },
                        },
                        {
                            $project: {
                                attribute_value: 1,
                                attribute_title: '$type_attribute.title',
                                attr_type: '$type_attribute.attr_type',
                            },
                        },
                        {
                            $unwind: '$attribute_title',
                        },
                        {
                            $unwind: '$attr_type',
                        },
                    ],
                },
            },
        ]);
        return result;
    }
    @Get('/colouredImageList')
    async getColouredImageList(@Query() query) {
        const { goods_id, color_id } = query;
        if (!goods_id) throw new BadRequestException('请传商品id');
        const aggregateOptions = [
            { $match: { goods_id: new mongoose.Types.ObjectId(goods_id), color_id: new mongoose.Types.ObjectId(color_id) } },
            { $sort: { sort: 1 } },
        ];
        if (!color_id) delete aggregateOptions[0].$match.color_id;
        const result = await this.goodsImageService.getModel().aggregate(aggregateOptions);
        if (!result.length) return this.goodsImageService.find({ goods_id });
        return result;
    }
}
